#!/bin/bash

# config
# db_user and db_password is saved in docker container config,here is not safely.
# container_id=""
container_id="a5d0fa9ee58d"
db_name="mytest"
save_dir="/home/jeremiah/code/shell/mysql-backup/files/"
file_name=$(date +'%Y-%m-%d_%H:%M:%S')'.sql.gz'
file_path=$save_dir$file_name

# check save dir
if [ ! -d $save_dir ];then
    mkdir $save_dir
fi
# splice command
command="mysqldump $db_name | gzip > $file_path"
if [ ! -z $container_id ];then
    command="docker exec $container_id "$command
fi
# run string command
eval $command
# history clear
find $save_dir -name '*.sql.gz' -type f -ctime +7 -exec rm -fr {} \; > /dev/null 2>&1